home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / Mesa-2.2 / src / bresenhm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-13  |  3.7 KB  |  190 lines

  1. /* $Id: bresenhm.c,v 1.1 1996/09/13 01:38:16 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  2.0
  6.  * Copyright (C) 1995-1996  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: bresenhm.c,v $
  26.  * Revision 1.1  1996/09/13 01:38:16  brianp
  27.  * Initial revision
  28.  *
  29.  */
  30.  
  31.  
  32. #include "bresenhm.h"
  33. #include "types.h"
  34.  
  35.  
  36. /*
  37.  * Evaluate Bresenham's integer line drawing algorithm.  Put each
  38.  * coordinate generated into x[] and y[] arrays.
  39.  *
  40.  * Input:  x1,y1 - coordinates of first endpoint
  41.  *         x2,y2 - coordinates of second endpoint
  42.  * Output:  x, y - array of coordinates generated by the algorithm
  43.  * Return:  number of values put into x[] and y[].
  44.  */
  45. GLuint gl_bresenham( GLcontext* ctx, GLint x1, GLint y1, GLint x2, GLint y2,
  46.              GLint x[], GLint y[] )
  47. {
  48.    register GLint dx, dy, xf, yf, a, b, c, i;
  49.  
  50.    if (x2>x1) {
  51.       dx = x2-x1;
  52.       xf = 1;
  53.    }
  54.    else {
  55.       dx = x1-x2;
  56.       xf = -1;
  57.    }
  58.  
  59.    if (y2>y1) {
  60.       dy = y2-y1;
  61.       yf = 1;
  62.    }
  63.    else {
  64.       dy = y1-y2;
  65.       yf = -1;
  66.    }
  67.  
  68. #define PLOT( X, Y )    x[i] = X;  y[i] = Y;
  69.  
  70.    if (dx>dy) {
  71.       a = dy+dy;
  72.       c = a-dx;
  73.       b = c-dx;
  74.       for (i=0;i<=dx;i++) {
  75.      PLOT( x1, y1 );
  76.          x1 += xf;
  77.          if (c<0) {
  78.             c += a;
  79.          }
  80.          else {
  81.             c += b;
  82.             y1 += yf;
  83.          }
  84.       }
  85.       return dx+1;
  86.    }
  87.    else {
  88.       a = dx+dx;
  89.       c = a-dy;
  90.       b = c-dy;
  91.       for (i=0;i<=dy;i++) {
  92.      PLOT( x1, y1 );
  93.          y1 += yf;
  94.          if (c<0) {
  95.             c += a;
  96.          }
  97.          else {
  98.             c += b;
  99.             x1 += xf;
  100.          }
  101.       }
  102.       return dy+1;
  103.    }
  104.  
  105. #undef PLOT
  106. }
  107.  
  108.  
  109.  
  110.  
  111. /*
  112.  * Evaluate Bresenham's line algorithm with stippling.
  113.  * Input:  x1, y1, x2, y2 - endpoints of line segment
  114.  * Output:  x, y - arrays of pixels along the line
  115.  *          mask - indicates draw/don't draw for each pixel
  116.  */
  117. GLuint gl_stippled_bresenham( GLcontext* ctx, GLint x1, GLint y1, GLint x2, GLint y2,
  118.                   GLint x[], GLint y[], GLubyte mask[] )
  119. {
  120.    GLint dx, dy, xf, yf, a, b, c, i;
  121.    GLushort m;
  122.  
  123.    if (x2>x1) {
  124.       dx = x2-x1;
  125.       xf = 1;
  126.    }
  127.    else {
  128.       dx = x1-x2;
  129.       xf = -1;
  130.    }
  131.  
  132.    if (y2>y1) {
  133.       dy = y2-y1;
  134.       yf = 1;
  135.    }
  136.    else {
  137.       dy = y1-y2;
  138.       yf = -1;
  139.    }
  140.  
  141. #define PLOT( X, Y )                            \
  142.     m = 1 << ((ctx->StippleCounter/ctx->Line.StippleFactor) & 0xf);    \
  143.     if (ctx->Line.StipplePattern & m) {                \
  144.         mask[i] = 1;                        \
  145.         x[i] = X;                        \
  146.         y[i] = Y;                        \
  147.     }                                \
  148.     else {                                \
  149.         mask[i] = 0;                        \
  150.     }                                \
  151.     ctx->StippleCounter++;
  152.  
  153.    if (dx>dy) {
  154.       a = dy+dy;
  155.       c = a-dx;
  156.       b = c-dx;
  157.       for (i=0;i<=dx;i++) {
  158.      PLOT( x1, y1 );
  159.          x1 += xf;
  160.          if (c<0) {
  161.             c += a;
  162.          }
  163.          else {
  164.             c += b;
  165.             y1 += yf;
  166.          }
  167.       }
  168.       return dx+1;
  169.    }
  170.    else {
  171.       a = dx+dx;
  172.       c = a-dy;
  173.       b = c-dy;
  174.       for (i=0;i<=dy;i++) {
  175.      PLOT( x1, y1 );
  176.          y1 += yf;
  177.          if (c<0) {
  178.             c += a;
  179.          }
  180.          else {
  181.             c += b;
  182.             x1 += xf;
  183.          }
  184.       }
  185.       return dy+1;
  186.    }
  187.  
  188. #undef PLOT
  189. }
  190.